﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars.Docking;
using DevExpress.XtraBars;

namespace zmMoniter
{
    public partial class Basefrm : DevExpress.XtraEditors.XtraForm
    {
        private DockPanel _fDockPanel = null;
        public DockPanel selfPanel = null;
        public Basefrm()
        {
            InitializeComponent();
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            if (_fDockPanel != null)
            {
                _fDockPanel.Text = this.Text;
                _fDockPanel.TabText = this.Text;
            }
        }

        public void DockToForm(DockManager manager, DockingStyle dock)
        {
            try
            {
                manager.BeginUpdate();
                if (manager == null || this.DesignMode) return;
                DockPanel setPanel = null;
                DockPanel samePanel = null;
                foreach (DockPanel panel in manager.Panels)
                {
                    if (panel.ControlContainer != null && panel.ControlContainer.Controls.Count == 1 && panel.ControlContainer.Controls[0].GetType().Equals(this.GetType()))
                        samePanel = panel;
                    else if (panel.Dock == dock)
                        setPanel = panel;
                }


                //foreach (DockPanel panel in manager.RootPanels)
                //{
                //    if (panel.Dock == dock)
                //        setPanel = panel;
                //}

                selfPanel = manager.AddPanel(dock);
                selfPanel.Width = 280;
                selfPanel.TabsPosition = DevExpress.XtraBars.Docking.TabsPosition.Left;
                this.FormBorderStyle = FormBorderStyle.None;
                this.MdiParent = null;
                this.TopLevel = false;
                this.Dock = DockStyle.Fill;
                this.Parent = selfPanel;
                selfPanel.ControlContainer.Controls.Add(this);
                if (samePanel != null)
                    selfPanel.DockAsTab(samePanel);
                else if (setPanel == null)
                    selfPanel.Dock = dock;
                else
                {
                    if (setPanel.Count > 0 && setPanel.Controls[0] is DockPanel)
                    {
                        selfPanel.DockAsTab(setPanel, -1);
                        //selfPanel.Dock = DockingStyle.Bottom;
                    }
                    else
                        selfPanel.DockAsTab(setPanel, -1);
                }

                selfPanel.BringToFront();
                if (selfPanel.ParentPanel != null)
                {
                    selfPanel.ParentPanel.BringToFront();
                    selfPanel.ParentPanel.ActiveChild = selfPanel;
                }
                selfPanel.Visibility = DockVisibility.Visible;
                _fDockPanel = selfPanel;
                _fDockPanel.ClosedPanel += ClosePanel;
                OnTextChanged(EventArgs.Empty);
            }
            finally
            {
                manager.EndUpdate();
            }
        }

        /// <summary>
        /// 当该窗口已经存在时，让其显示出来
        /// </summary>
        /// <param name="_DM">父容器</param>
        public void Show2(DockManager _DM)
        {
            _DM.ActivePanel = this.selfPanel;
        }
        private void ClosePanel(object sender, DockPanelEventArgs e)
        {
            this.Close();
            _fDockPanel.DockManager.RemovePanel(_fDockPanel);
        }
    }
}
